<template>
  <div
    :id="id"
    :class="className"
    :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
import resize from '@/components/Charts/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    },
    data: Array
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    data: {
      handler(val) {
        if (val.length) {
          this.$nextTick(_ => {
            this.initChart()
          })
        }
      },
      deep: true,
      immediate: true
    }
  },
  // mounted() {
  //   this.initChart()
  // },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      if (!this.chart) {
        this.chart = echarts.init(document.getElementById(this.id))
      }

      const options = {
        grid: {
          left: 0,
          top: 0,
          right: 15,
          bottom: 10,
          containLabel: true
        },
        xAxis: {
          type: 'value',
          minInterval: 1,
          axisLabel: {
            color: '#1897cf',
            fontSize: 14
          },
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            lineStyle: {
              color: '#152e55'
            }
          }
        },
        yAxis: {
          type: 'category',
          data: this.data.map(item => item.lineName),
          axisLabel: {
            color: '#43aef8',
            fontSize: 14,
            margin: 15
          },
          axisLine: {
            lineStyle: {
              color: '#152e55'
            }
          },
          axisTick: {
            show: false
          }
        },
        series: [
          {
            data: this.data.map(item => item.predictNum),
            type: 'bar',
            barWidth: 20,
            itemStyle: {
              color: '#c7871e'
            }
          }
        ]
      }
      this.chart.setOption(options)
    }
  }
}
</script>
